﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.dataaccess;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using InHD.commands;
using System.Windows;
using InHD.Properties;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class CustomerViewModel: WorkspaceViewModel
    {
        #region Constructor
        public CustomerViewModel()
        {
            this._customerRepository = new CustomerRepository();
            this._customer = Customer.CreateCustomer();
            LoadCustomerList();
            this.PageContent = new CustomerListViewModel(this._customerRowCollection);
            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
        }
        #endregion

        #region Value Property
        private Customer _customer;
        readonly CustomerRepository _customerRepository;
        private WorkspaceViewModel _pageContent;
        private List<Customer> _customerRowCollection = new List<Customer>();
        private int _rowIndex = -1;
        #endregion

        #region Presentation Property

        public WorkspaceViewModel PageContent
        {
            get
            {
                return _pageContent;
            }
            set
            {
                _pageContent = value;
            }
        }

        #endregion

        #region Normal Method
        private void LoadCustomerList()
        {
            this._customerRepository.LoadAllCustomers();
            this._customerRowCollection = this._customerRepository.GetCustomers();
        }

        private void HandleCloseCurrentPage(object source, EventArgs e)
        {
            this.PageContent.RequestClose -= this.HandleCloseCurrentPage;
            if (this.PageContent is CustomerListViewModel)
            {
                string commandExcuted = ((CustomerListViewModel)PageContent).commandExcute;
                if (string.IsNullOrEmpty(commandExcuted))
                {
                    OnRequestClose();
                    return;
                }

                if (commandExcuted.Equals("View"))
                {
                    this._customer = ((CustomerListViewModel)PageContent).SelectedCustomer.CreateShallowCopy();
                    this.PageContent = new CustomerFormViewModel(ActionModes.Edit, _customer);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Add"))
                {
                    this._customer = Customer.CreateCustomer();
                    this.PageContent = new CustomerFormViewModel(ActionModes.New, _customer);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
            else if (this.PageContent is CustomerFormViewModel)
            {
                string commandExcuted = ((CustomerFormViewModel)PageContent).commandExcute;
                if (commandExcuted.Equals("Close"))
                {
                    this._customer = null;
                    this.PageContent = new CustomerListViewModel(_customerRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Save"))
                {
                    if (((CustomerFormViewModel)PageContent).FormMode.Equals(ActionModes.New))
                    {
                        string result = this._customerRepository.AddCustomer(((CustomerFormViewModel)PageContent).Customer);
                        if (string.IsNullOrEmpty(result))
                        {
                            _customerRowCollection = _customerRepository.GetCustomers();
                            this.PageContent = new CustomerListViewModel(_customerRowCollection);
                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new CustomerFormViewModel(ActionModes.New, ((CustomerFormViewModel)PageContent).Customer);
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new CustomerFormViewModel(ActionModes.New, ((CustomerFormViewModel)PageContent).Customer);
                        }
                    }
                    else
                    {
                        string result = this._customerRepository.EditCustomer(((CustomerFormViewModel)PageContent).Customer);
                        if (string.IsNullOrEmpty(result))
                        {
                            _customerRowCollection = _customerRepository.GetCustomers();
                            this.PageContent = new CustomerListViewModel(_customerRowCollection);
                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new CustomerFormViewModel(ActionModes.Edit, ((CustomerFormViewModel)PageContent).Customer);
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new CustomerFormViewModel(ActionModes.Edit, ((CustomerFormViewModel)PageContent).Customer);
                        }
                    }
                    
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Delete"))
                {
                    if (!string.IsNullOrEmpty(_customerRepository.DeleteCustomer(((CustomerFormViewModel)PageContent).Customer.CustomerUNK)))
                    {
                        MessageBox.Show(Resources.InHD_DB_Error);
                    }
                    else
                    {
                        _customerRowCollection = null;
                        _customerRowCollection = _customerRepository.GetCustomers();
                    }

                    this.PageContent = new CustomerListViewModel(_customerRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
        }
        #endregion

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Customer;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

        }
    }
}
